<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html ng-app="app" id="ng-app">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" >
<meta charset="UTF-8">
<%@include file="/include/style.jsp"%>
<%@include file="/include/script.jsp"%>
<script type="text/javascript">
function UserCtrl($scope, $http){
	$scope.username = '${requestScope.username}';
	
	$scope.errorMsg = {};
	
	$scope.init = function() {
		$scope.mode = $scope.username && 'update' || 'create';
		
		$http.post("/role/findAll").success(function(response) {
			if (response.success) {
				$scope.roles = response.data || [];
				
				$scope.roles.remove(function(i){
					return i.id == 3;
				});
				
				if ($scope.mode == 'update') {
					$http.post("/user/get/"+$scope.username).success(function(response) {
						if (response.success) {
							$scope.user = response.data || {};
						} else {
							Message.error("错误", response.message || '数据加载失败！');
						}
					});
				} else {
					$scope.user = {};
				}
			} else {
				Message.error("错误", response.message || '数据加载失败！');
			}
		});
	};
	
	$scope.init();
	
	$scope.isUsernameInvalid = function(){
		$scope.errorMsg.username = undefined;
		var username = $scope.user.username;
		
		if (!username) {
			$scope.errorMsg.username =  "用户名不能为空!";
		} else if (username.length < 6) {
			$scope.errorMsg.username =  "用户名长度不能小于6位!";
		} else if (!/^[A-Za-z0-9]+$/.test(username)) {
			$scope.errorMsg.username =  "用户名只能由数字与字母组成!";
		}
		
		return $scope.errorMsg.username;
	};
	
	$scope.isPasswordInvalid = function(){
		$scope.errorMsg.password = undefined;
		var password = $scope.password;
		
		if (!password && $scope.mode == 'create') {
			$scope.errorMsg.password = "密码不能为空";
		} else if(password && password.length < 6){
			$scope.errorMsg.password = "密码长度不能小于6位";
		} else if(password && password.containsSpacialChar()) {
			$scope.errorMsg.password = "密码不能出现\" ' < > & ? # =特殊字符！";
		}
		
		return $scope.errorMsg.password || ($scope.comfirmPwd && $scope.isConfirmPwdInvalid());
	};
	
	$scope.isConfirmPwdInvalid = function() {
		$scope.errorMsg.comfirmPwd = undefined;
		
		if (!$scope.comfirmPwd && ($scope.mode == 'create' || $scope.password)) {
			$scope.errorMsg.comfirmPwd = "确认密码不能为空";
		} else if ($scope.comfirmPwd && $scope.password != $scope.comfirmPwd) {
			$scope.errorMsg.comfirmPwd = "两次输入的密码不一致";
		}
		
		return $scope.errorMsg.comfirmPwd;
	};
	
	$scope.isEmailInvalid = function(){
		$scope.errorMsg.email = undefined;
		if (!$scope.user.email) {
			$scope.errorMsg.email = "邮箱不能为空";
		} else if (!/\w@\w*\.\w/.test($scope.user.email)){
			$scope.errorMsg.email = "邮箱格式有误";
		} else if (/[;,]/.test($scope.user.email)) {
			$scope.errorMsg.email = "邮箱中不能含有; , 等分隔符";
		}
		
		return $scope.errorMsg.email;
	};
	
	$scope.isRoleInvalid = function(){
		$scope.errorMsg.role = undefined;
		if (!$scope.user.roleId) {
			$scope.errorMsg.role = "请选择用户角色！";
		}
		
		return $scope.errorMsg.role;
	};
	
	$scope.isFormInvalid = function() {
		var result1 = $scope.isUsernameInvalid();
		var result2 = $scope.isPasswordInvalid();
		var result3 = $scope.isConfirmPwdInvalid();
		var result4 = $scope.isEmailInvalid();
		var result5 = $scope.isRoleInvalid();
		
		return result1 || result2 || result3 || result4 || result5;
		
	};
	
	$scope.save = function() {
		if ($scope.isFormInvalid()) {
			return;
		}
		
		if ($scope.password) {
			$scope.user.password = $scope.password;
		}
		
		$http.post("/user/" + $scope.mode, $scope.user).success(function(response){
			if(response.success){
				$scope.backPage();
			}else{
				Message.error("提示", response.message);
			}
		});	
	};
	
	$scope.backPage = function(){
		window.location.href = "/user/index";
	};
};

app.controller('UserCtrl', UserCtrl);
</script>
</head>
<body ng-controller="UserCtrl">
<form class="form-horizontal create_form">
<legend>
	{{mode == 'create' && '新建用户' || '编辑用户'}}
</legend>
<div class="form-container form-horizontal">
	<div class="form-group">
		<label class="col-sm-1 control-label">
			<span class="star-icon">*</span>用户名：
		</label>
		
		<div class="col-sm-4">
			<input type="text" class="form-control" ng-model="user.username" placeholder="必须填写,长度6-24位,数字与字母组成" 
				ng-disabled="mode == 'update'" ng-change="isUsernameInvalid()" maxlength="24"/>
		</div>
		
		<div class="col-sm-7 form-control-static">
			<span class="text-danger">{{errorMsg.username}}</span>
		</div>
	</div>
	
	<div class="form-group">
		<label class="col-sm-1 control-label">
			<span class="star-icon" ng-show="mode == 'create'">*</span>{{mode == 'create' && '用户密码' || '用户新密码'}}：
		</label>
		
		<div class="col-sm-4">
			<input type="password" class="form-control" ng-model="password" placeholder="{{mode == 'create' && '必须填写,' || ''}}长度6-24位" 
				ng-change="isPasswordInvalid()" maxlength="24" />
		</div>
		
		<div class="col-sm-7 form-control-static">
			<span class="text-danger">{{errorMsg.password}}</span>
		</div>
	</div>
	
	<div class="form-group">
		<label class="col-sm-1 control-label">
			<span class="star-icon" ng-show="mode == 'create'">*</span>{{mode == 'create' && '确认密码' || '确认新密码'}}：
		</label>
		
		<div class="col-sm-4">
			<input type="password" class="form-control" ng-model="comfirmPwd" placeholder="与密码一致" ng-change="isConfirmPwdInvalid()"/>
		</div>
		
		<div class="col-sm-7 form-control-static">
			<span class="text-danger">{{errorMsg.comfirmPwd}}</span>
		</div>
	</div>
	
	<div class="form-group">
		<label class="col-sm-1 control-label">
			<span class="star-icon">*</span>邮箱：
		</label>
		
		<div class="col-sm-4">
			<input type="text" class="form-control" ng-model="user.email" placeholder="必须填写" ng-change="isEmailInvalid()"/>
		</div>
		
		<div class="col-sm-7 form-control-static">
			<span class="text-danger">{{errorMsg.email}}</span>
		</div>
	</div>
	
	<div class="form-group">
		<label class="col-sm-1 control-label">
			<span class="star-icon">*</span>角色：
		</label>
		
		<div class="col-sm-4">
			<select class="form-control" 
					ng-model="user.roleId" 
					ng-options="role.id as role.name for role in roles"
					ng-change="isRoleInvalid()">
				<option value="">选择角色</option>
			</select>
		</div>
		
		<div class="col-sm-7 form-control-static">
			<span class="text-danger">{{errorMsg.role}}</span>
		</div>
	</div>
	
	<div class="form-group">
		<div class="col-sm-offset-1">
			<input type="button" class="btn btn-success" value="保存" ng-click="save()"> 
			<input type="button" class="btn btn-default" value="返回" ng-click="backPage()" >
		</div>
	</div>
</div>
</form>
</body>
</html>